add_custom_target(sigslot-tests
    COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
    COMMENT "Build and run all the unit tests.")

macro(pal_create_test target ut)
    add_executable(${target} EXCLUDE_FROM_ALL "${ut}")
    add_test(${target} ${target})
    add_dependencies(sigslot-tests ${target})
    sigslot_set_properties(${target} PRIVATE)
    target_link_libraries(${target} PRIVATE Pal::Sigslot)
    target_compile_definitions(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>)
endmacro()

file(GLOB_RECURSE UNIT_TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cpp")
foreach(ut IN LISTS UNIT_TESTS)
    string(REPLACE ".cpp" "" target ${ut})
    string(REGEX REPLACE "/" "." target ${target})
    set(target sigslot-test-${target})

    if (target MATCHES "boost")
        if (TARGET Boost::system)
            pal_create_test(${target} "${ut}")
            target_link_libraries(${target} PRIVATE Boost::system)
        endif()
    elseif (target MATCHES "qt")
        if (TARGET Qt5::Core)
            pal_create_test(${target} "${ut}")
            target_link_libraries(${target} PRIVATE Qt5::Core)
            set_target_properties(${target} PROPERTIES AUTOMOC ON)
        endif()
    else()
        pal_create_test(${target} "${ut}")
    endif()
endforeach()
